package com.jinbai.servlet.user;


import com.jinbai.service.UserService;
import com.jinbai.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/studio/user/login.do")
public class LoginDoServlet extends HttpServlet {
    private UserService userService = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        final String username = req.getParameter("username");
        final String password = req.getParameter("password");
        if (username == null || username.trim().isEmpty() || password == null || password.trim().isEmpty()) {
            resp.sendRedirect("/ssm/wabapp/studio/user/login.html");
            return;
        }
        UserVO userVO = userService.loginUser(username, password);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/plain");
        final PrintWriter writer = resp.getWriter();
        if (userVO == null) {
            writer.println("登录失败");
            resp.sendRedirect("/studio/user/login.html");
        } else {
            final HttpSession session = req.getSession();
            session.setAttribute("currentUser", userVO);
            writer.println("登录成功");
            resp.sendRedirect("/studio/album/list.html");
        }
    }
}
